package demo

import (
	"context"

	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type HelloServiceGrpcServer struct{}

func (HelloServiceGrpcServer) Hello(*gin.Context, *HelloServiceHelloReq) (*HelloServiceHelloRsp, error) {
	return nil, status.Errorf(codes.Unimplemented, "method Hello not implemented")
}

func (i HelloServiceGrpcServer) GrpcServiceDesc() *grpc.ServiceDesc {
	return &grpc.ServiceDesc{
		ServiceName: "demo.HelloService",
		HandlerType: (*HelloServiceGrpcServerInterface)(nil),
		Methods: []grpc.MethodDesc{
			{
				MethodName: "Hello",
				Handler:    HelloServiceHelloHandler,
			},
		},
		Streams:  []grpc.StreamDesc{},
		Metadata: "demo/HelloService.proto",
	}
}

func HelloServiceHelloHandler(srv any, ctx context.Context, dec func(any) error, interceptor grpc.UnaryServerInterceptor) (any, error) {
	in := new(HelloServiceHelloReq)
	if err := dec(in); err != nil {
		return nil, err
	}
	if interceptor == nil {
		return srv.(HelloServiceGrpcServer).Hello(ctx.(*gin.Context), in)
	}
	info := &grpc.UnaryServerInfo{
		Server:     srv,
		FullMethod: HelloService_Hello_FullMethodName,
	}
	handler := func(ctx context.Context, req any) (any, error) {
		return srv.(HelloServiceGrpcServerInterface).Hello(ctx.(*gin.Context), req.(*HelloServiceHelloReq))
	}
	return interceptor(ctx, in, info, handler)
}

type HelloServiceGrpcServerInterface interface {
	Hello(*gin.Context, *HelloServiceHelloReq) (*HelloServiceHelloRsp, error)
}
